#include "grflib_stdafx.h"
#include <DXUT.h>
#include <SDKmisc.h>
#include <ITraverse.h>
#include "../include/TextLabelManager.h"

namespace grflib { namespace engine { namespace aux {
using namespace grflib::engine::core;

CTextLabelManager::CTextLabelManager()
    : m_pTextHelper(0),
    m_pFont(0),
    m_pSprite(0)
{
}

CTextLabelManager::~CTextLabelManager()
{
    OnDestroy();
}

HRESULT CTextLabelManager::OnCreateDevice(ID3D10Device *pd3dDevice, const DXGI_SURFACE_DESC *pBufferSurfaceDesc)
{
	HRESULT hr;
    V_RETURN( D3DX10CreateFont(pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
							   OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
							   _T("Arial"), &m_pFont) );
	V_RETURN( D3DX10CreateSprite(pd3dDevice, 512, &m_pSprite) );
	m_pTextHelper = new CDXUTTextHelper(0, 0, m_pFont, m_pSprite, 15);
    
    return S_OK;
}

void CTextLabelManager::OnDestroy()
{
    SAFE_RELEASE(m_pFont);
    SAFE_RELEASE(m_pSprite);
    SAFE_DELETE(m_pTextHelper);
}

HRESULT CTextLabelManager::OnRender(ID3D10Device *pd3dDevice, double fTime, float fElapsedTime)
{
    if (!m_pTextHelper)
        return E_FAIL;
    m_pTextHelper->Begin();
    int x, y;
    HRESULT hr;
    BOOST_FOREACH(const obj_ref_type obj, m_store)
    {
        obj.second->GetPosition(x, y);
        m_pTextHelper->SetInsertionPos(x, y);
        V_RETURN( m_pTextHelper->DrawTextLine(obj.second->GetString().c_str()) );
    }
    m_pTextHelper->End();
    return S_OK;
}

}}} // namespaces